package l1j.opqlo.NewSystem;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.Thread.PcOtherThreadPool;
import l1j.server.server.GetNowTime;
import l1j.server.server.utils.StreamUtil;

public class 各項記錄輸出 {
	private static final Logger _log = Logger.getLogger(各項記錄輸出.class.getName());


	/**自動創建需要的文件夾*/
	public static void setLocation(){
		new File(Config.LOG_LOCATION+"記錄/AI檢測").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/小G").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/元寶紀錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/加速器檢測").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/丟棄物品").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/交易記錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/刪除物品").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/金幣金條").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/穿牆玩家記錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/倉庫/血盟倉庫").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/倉庫/妖精倉庫").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/倉庫/個人倉庫").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/個人商店").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/海賊拉霸").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/特殊商店/支票").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/特殊商店/特殊１").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/特殊商店/特殊２").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/特殊商店/商幣").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/特殊商店/推推幣").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/給予物品").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/對話紀錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/噴裝").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/獎勵發放").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/撿取物品").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/獲得道具").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/轉換升級記錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/鑑定記錄").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/強化失敗").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/精煉師").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/骰寶").mkdirs();
		new File(Config.LOG_LOCATION+"記錄/移民").mkdirs();
	}
	private static final HashMap<Integer , StringBuilder>_logMap = new HashMap<Integer , StringBuilder>();
	public void startLog(){
//		getTime();
		_time = new LogTimer();
		_time.start();
	}
	public static void setAllLog(){
		_time.saveAllLog();
	}
//	private static void getTime(){
//		tz = TimeZone.getTimeZone(Config.TIME_ZONE);
//		cal = Calendar.getInstance(tz);
//		year = cal.get(Calendar.YEAR) - 2000;
//		_GetNowTime.GetFormatDate() = cal.get(Calendar.GetNowTime.GetFormatDate());
//		final StringBuilder timer = new StringBuilder().append(year).append("-").append(cal.get(Calendar.MONTH) + 1).append("-").append(_GetNowTime.GetFormatDate());
//		GetNowTime.GetFormatDate() = GetNowTime.GetFormatGetNowTime.GetFormatDate()();
//	}
	private static LogTimer _time;
//	private static String GetNowTime.GetFormatDate();
//	private static int _GetNowTime.GetFormatDate();
//	private static TimeZone tz;
//	private static Calendar cal;
//	private static int year ;

	private static final String SP = "\r\n▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇\r\n";

	public static void LogSave(final int type, final String info) {
		StringBuilder log = _logMap.get(type);
		if(log == null){
			log = new StringBuilder(1024);
			_logMap.put(type, log);
		}
		boolean sp = false;
		switch (type) {
		/*FileWriter(String fileName , boolean append )
		 * 以fileName的字串內容為檔案資料來源，建構出FileWriter物件，
		 * 如果設定append為true，則寫入的資料會加在原檔案資料後方*/
		case 3:// OwnStores
			sp = true;
			break;
		case 4:// ElfWarehouse
			sp = true;
			break;
		case 5:// OwnWarehouse
			sp = true;
			break;
		case 6:// ClanWarehouse
			sp = true;
			break;
		case 7:// SmallGM
			sp = true;
			break;
		case 8:// TradeG
			sp = true;
			break;
		case 9:// TradeA
			sp = true;
			break;
		case 19:
			sp = true;
			break;
		case 20:
			sp = true;
			break;
		case 21:
			sp = true;
			break;
		case 22:// TradeA
			sp = true;
			break;
		case 23:// TradeA
			sp = true;
			break;
		case 25:
			sp = true;
			break;
		case 30:
			sp = true;
			break;
		case 33:
			sp = true;
			break;
		case 35:
			sp = true;
			break;
		default:
			break;
		}
		log.append(info);
		if (sp) {
			log.append(SP);
		}else{
			log.append("\r\n");
		}
	}
	private class LogTimer extends TimerTask {

		private ScheduledFuture<?> _timer;
		private void start() {
			final int timeMillis = 30000;
			this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this,timeMillis, timeMillis);
		}
		@Override
		public void run() {
			try{
					setLog();
			} catch (final Exception e) {
				_log.log(Level.SEVERE , e.getLocalizedMessage() , e);
				PcOtherThreadPool.get().cancel(this._timer, false);
				LogTimer log = new LogTimer();
				log.start();
			}
		}
		private void saveAllLog(){
			PcOtherThreadPool.get().cancel(this._timer, false);
			setLog();
		}

		private void setLog(){
			try{
				Set<Integer>set = _logMap.keySet();
				Iterator<Integer> ite = set.iterator();
				final HashMap<Integer , StringBuilder>map = new HashMap<Integer , StringBuilder>();
				while(ite.hasNext()){
					final int key = ite.next();
					StringBuilder msg = _logMap.get(key);
					if(msg.length()>0){
						map.put(key, msg);
					}
				}
				if(!map.isEmpty()){
					BufferedWriter out = null;
					try {
						set = map.keySet();
						ite = set.iterator();
						while(ite.hasNext()){
							final int key = ite.next();
							StringBuilder msg = map.get(key);
							switch (key) {
							/*FileWriter(String fileName , boolean append )
							 * 以fileName的字串內容為檔案資料來源，建構出FileWriter物件，
							 * 如果設定append為true，則寫入的資料會加在原檔案資料後方*/

							case 1:// box 開箱記錄
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/獲得道具/開箱"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 3:// OwnStores
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/個人商店/商店買賣"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 4:// ElfWarehouse
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/倉庫/妖精倉庫/妖倉提領"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 5:// OwnWarehouse
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/倉庫/個人倉庫/個倉提領"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 6:// ClanWarehouse
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/倉庫/血盟倉庫/盟倉提領"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 7:// SmallGM
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/小G/創造"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 8:// TradeG
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/小G/交易"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 9:// TradeA
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/交易記錄/交易"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 10:// drop
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/噴裝/噴裝"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 11:// savemoney
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/金幣金條/金條"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 12:// takemoney
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/金幣金條/金幣"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 13:// 刪除物品
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/刪除物品/刪除"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 14:// 角色改名
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/改名清單.txt", true));
								break;
							case 15:// 刪除角色
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/刪除角色.txt", true));
								break;
							case 16:// 裝備爆掉
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/強化失敗/爆掉"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 17:// 打寶記錄
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/獲得道具/掉落"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 18:// 積分領取記錄
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/獎勵發放/獎勵"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 19:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/卡點記錄".toString()+ ".txt", true));
								break;
							case 20:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/鑑定記錄/鑑定記錄"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 21:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/對話記錄/信件記錄"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 22:// TradeA
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/轉換升級記錄/轉換記錄"
										+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 23:// TradeA
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/轉換升級記錄/升級記錄"
										+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 24:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/穿牆玩家記錄/穿牆記錄"
										+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 25:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/轉換升級記錄/祝福記錄"
										+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 26:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/丟棄物品/丟棄"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 27:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/撿取物品/撿取"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 28:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/給予物品/給予"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 29:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/刮刮樂.txt", true));
								break;
							case 30:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/自動贊助.txt", true));
								break;
							case 31:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/刮刮樂.txt", true));
								break;
							case 32:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/元寶紀錄/元寶"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 33:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/移民/移民"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 34:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/精煉師/強化"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							case 35:
								out = new BufferedWriter(new FileWriter(Config.LOG_LOCATION+"記錄/骰寶/骰寶紀錄"+ GetNowTime.GetFormatDate() + ".txt", true));
								break;
							default:
								return;
							}
							if(out !=null){
								out.write(msg.toString());
								msg.delete(0, msg.length());
								out.flush();
								StreamUtil.close(out);
								out = null;
							}
						}
					} catch (final IOException e) {
						_log.log(Level.SEVERE, e.getLocalizedMessage(), e); 
						System.out.println("各項紀錄:IO錯誤");
					}finally{
						StreamUtil.close(out);
					}
				}
			} catch (final Exception e) {_log.log(Level.SEVERE, e.getLocalizedMessage(), e); System.out.println("其他錯誤");}
		}
	}
}
